# -*- coding: utf-8 -*-
'''
Created on Apr 23, 2011

@author: "Yang Sun"
'''
import time
import common
from program import Program
from server import Server
from httputil import *

type = [{'Title':'电影', 'path':''}, 
        {'Title':'电视剧', 'path':''}]

auth = '&auth=d410fafad87e7bbf6c6dd62434345818'
root = 'http://vod.150hi.com/'
searchRoot = 'http://search.150hi.com/'    
catalog = root + 'tags-ex.ashx?platform=silverlight&type=%s&dimension=catalog&forbidvip=1' + auth
area = root + 'tags-ex.ashx?platform=silverlight&type=%s&dimension=area&forbidvip=1' + auth    
year = root + 'tags-ex.ashx?platform=silverlight&type=%s&dimension=year&forbidvip=1' + auth
act = root + 'tags-ex.ashx?platform=silverlight&type=%s&dimension=act&forbidvip=1' + auth
director = root + 'tags-ex.ashx?platform=silverlight&type=%s&dimension=director&forbidvip=1' + auth
listCountInPage = 18
list = root + 'list-ex.ashx?platform=silverlight&forbidvip=1&c=' + str(listCountInPage) + '&s=%s&type=%s&tag=%s' + auth    
            

def getFilter(typeid):
    #get category filter
    url = catalog % typeid        
    html = getHttpDataCompact(url)
    categoryTag = getTagHelper(html)
    
    #get area filter
    url = area % typeid        
    html = getHttpDataCompact(url)
    areaTag = getTagHelper(html)
    
    #get year filter
    url = year % typeid        
    html = getHttpDataCompact(url)
    yearTag = getTagHelper(html)        
    '''
    #get director filter
    url = director % typeid        
    html = getHttpDataCompact(url)
    directorTag = getTagHelper(html)
    
    #get actor filter
    url = act % typeid        
    html = getHttpDataCompact(url)
    actTag = getTagHelper(html)
    '''
    
    return {'分类': categoryTag, '地区': areaTag, '年份': yearTag}

def getProgram(typeid=1, tags='|||||'):
    url = createListUrl(typeid, tags)
    
    html = getHttpDataCompact(url)
    match = re.compile('<v><vid>([^<]*)</vid><playlink><\!\[CDATA\[([^\]]*)\]\]></playlink><title>([^<]*)</title><type>([^<]*)</type><catalog>([^<]*)</catalog><director>([^<]*)</director><act>([^<]*)</act><year>([^<]*)</year><area>([^<]*)</area><imgurl>([^<]*)</imgurl><state>([^<]*)</state><note>([^<]*)</note><mark>([^<]*)</mark><hot>([^<]*)</hot><pv>([^<]*)</pv><bitrate>([^<]*)</bitrate><resolution>([^<]*)</resolution><flag>([^<]*)</flag><duration>([^<]*)</duration></v>').findall(html)
    programs = []
    index = 0;    
    while index < len(match):           
        prog = Program(
         match[index][0],
         match[index][1],
         match[index][2],
         match[index][3],
         match[index][4],
         match[index][5],
         match[index][6],
         match[index][7],
         match[index][8],
         match[index][9],
         match[index][10],
         match[index][11],
         match[index][12],
         match[index][13],
         match[index][14],
         match[index][15],
         match[index][16],
         match[index][17],
         match[index][18]
         )                 
        
        programs.append(prog)         
        index = index + 1
                    
    return programs

def getServer(program):                             
    url = 'http://jump.g1d.net/%sdt' % program.file_name             
    html = getHttpDataCompact(url)
    match = re.compile('<server_host>([^<]*)</server_host><server_time>([^<]*)</server_time><id>([^<]*)</id><BWType>([^<]*)</BWType>').findall(html)                
    if len(match) > 0:
        server = Server(match[0][0], match[0][1], match[0][2], match[0][3])
        return server             
    return None

def getDragUrl(program):                
    url = 'http://drag.g1d.net/%s0drag' % program.file_name
    dragInfo = {}        
    html = getHttpDataCompact(url)
    match = re.compile('<segmentno="([^"]+)"offset="([^"]+)"headlength="([^"]+)"duration="([^"]+)"varid="([^"]+)"filesize="([^"]+)"/>').findall(html)
    dragInfo['segCount'] = len(match)
    return dragInfo

def getVideoUrl(program, server, drag_info):
    ct = time.mktime(time.gmtime(time.time()))
    tConst = time.mktime(time.strptime('1970/01/01', '%Y/%m/%d'))
    relativeServerTime = int(ct + server.server_time - server.local_time - tConst)            
    key = common.getKey(relativeServerTime)
    url_collection = []
    index = 0
    while index < int(drag_info['segCount']):
        url = 'http://%s:8080/%d/%s?key=%s' % (server.host, index, program.file_name, key)
        url_collection.append(url)
        index += 1        
    return url_collection 

########### helper functions ########### 
def getTagHelper(html):
    tag = {}
    match = re.compile('<tag><name>([^<]*)</name><count>([^<]*)</count></tag>').findall(html)
    index = 0;    
    while index < len(match):
        tag[match[index][0]] = match[index][1]                   
        index = index + 1        
    return tag

def createListUrl(typeid, tags):        
        url = list % (str(1), typeid, tags)
        return url
    
if __name__ == "__main__":
    pass    
